<?php
/**[属性试图模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-11-11 11:09:33
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-12-27 16:52:14
 */
class AttrViewModel extends ViewModel{
	protected $tableName = 'attr';

	public $viewFields  = array(
		'attr'=>array(
			'sort','attr_name',
			'_type'=>'INNER',
		),
		'article_attr'=>array(
			'*',
			'_type'=>'INNER',
			'_on' =>'attr.attr_id=article_attr.attr_attr_id',
		),
		

	);


	/**
	 * [get_filter 获取属性筛选]
	 * @return [type] [description]
	 */
	public function get_filter($cid)
	{

		$categoryModel = D('Category');

		// 获取所有的子cid
		$childCid = $categoryModel->get_child_cid($cid);
		$where['category_cid'] = array('in',$childCid) ;


		$data = $this->group('attr_attr_id')->where($where)->select();
		//p($data);die;
		if(!$data)
			return false;
		$result = array();
		foreach ($data as $k=>$v)
		{
			// 属性名称
			$temp = array(

				'attr_name'=>$v['attr_name'],
				'attr_id'=>$v['attr_attr_id']
			);



			// 属性值
			$where['attr_attr_id']=$v['attr_attr_id'];
			$attrValue = $this->group('attr_value_attr_value_id')->where($where)->select();

			if($attrValue)
			{

				//全部
				$wherea['category_cid'] = array('in',$childCid) ;
				$temp['attr_value'][] = array(
					'attr_value'=>'All',
					'attr_value_id'=>0,
					'count'=>''
				);	


				// 每一个属性值
				foreach($attrValue as $value)
				{
					
					$wherea['attr_value_attr_value_id'] = $value['attr_value_attr_value_id'];
					$count = $this->where($wherea)->count();
					$temp['attr_value'][] = array(
						'attr_value'=>$value['attr_value'],
						'type'=>$value['type'],
						'attr_value_id'=>$value['attr_value_attr_value_id'],
						'count'=>$count,
					);
				}


			}

			$result[]= $temp;
		}

		return $result;
		
	}

}